<template>

  <div class="app-container">
    <!-- 角色管理内容 -->
    <div class="role-operate">
      <el-button size="mini" type="primary" @click="showDialog=true">添加角色</el-button>
    </div>
    <!-- 放置table组件 -->
    <el-table :data="list">
      <!-- 放置列 -->

      <el-table-column prop="name" align="center" width="200" label="角色">
        <template v-slot="{row}">
          <el-input v-if="row.isEdit" v-model="row.editRow.name" type="text" />
          <span v-else>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="state" align="center" width="200" label="启用">
        <!-- 自定义列结构 -->
        <template v-slot="{ row }">
          <el-switch
            v-if="row.isEdit"
            v-model="row.editRow.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
          <span v-else>
            {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="description" align="center" label="描述">
        <template v-slot="{row}">
          <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
          <span v-else>{{ row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <!-- 放置操作按钮 -->
        <template v-slot="{row}">
          <template v-if="row.isEdit">
            <el-button size="mini" type="text" @click="sureEdit(row)">确认</el-button>
            <el-button size="mini" type="text" @click="handleEdit(row)">取消</el-button>
          </template>
          <template v-else>
            <el-button size="mini" type="primary" @click="doAssign(row.id)">分配权限</el-button>
            <el-button size="mini" type="primary" @click="btnEditRow(row)">编辑</el-button>
            <el-popconfirm
              confirm-button-text="好的"
              cancel-button-text="不用了"
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="delRow(row.id)"
            >
              <el-button slot="reference" size="mini" type="primary">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>

    </el-table>
    <!-- 放置分页组件 -->
    <el-row type="flex" style="height:60px" align="middle" justify="end">
      <!-- 放置分页组件 -->
      <el-pagination
        :page-size="pagesize"
        :current-page="page"
        layout="prev, pager, next"
        :total="total"
        @current-change="changePage"
        @size-change="changePageSize"
      />
    </el-row>

    <!-- 复用弹窗 -->
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="dataForm" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="formData.state" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="isOK">确定</el-button>
              <el-button size="mini" @click="handleClose">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分配编辑权限组件 -->
    <AssignPermission
      ref="AssignPermission"
      :show-assign-dialog.sync="showAssignDialog"
      :current-id="currentId"
    />

  </div>

</template>
<script>
import { getRoles, addRoles, editRoles, delRoles } from '@/api/roles'
import AssignPermission from './components/assignPermission.vue'
export default {
  name: 'Role',
  components: { AssignPermission },
  data() {
    return {
      list: [],
      page: 1,
      pagesize: 20,
      total: 0,
      showDialog: false,
      showAssignDialog: false,
      currentId: -1,
      formData: {
        name: '',
        description: '',
        state: 1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '角色名称的长度为2-10个字符', trigger: 'blur' }
        ],
        description: [{ required: true, message: '角色介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '角色介绍的长度为1-100个字符', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUsers()
  },
  methods: {
    async getUsers() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      const { rows, total } = res.data
      console.log(rows, total)
      this.list = rows
      this.total = total
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          id: item.id,
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    },
    changePage(newPage) {
      console.log(newPage)
      this.page = newPage
      console.log(this.page)
      this.getUsers()
    },
    changePageSize(pages) {
      this.pagesize = pages
      this.pages = 1
      this.getUsers()
    },
    handleClose() {
      this.$refs.dataForm.resetFields()
      this.showDialog = false
      this.formData = {
        name: '',
        description: '',
        state: ''
      }
    },
    async addManager() {
      const res = await addRoles(this.formData)
      console.log(res)
      this.$message.success('添加角色成功')
    },
    isOK() {
      this.$refs.dataForm.validate(async valid => {
        if (valid) {
          await this.addManager()
          this.handleClose()
          this.total += 1
          this.page = Math.ceil(this.total / this.pagesize)
          this.getUsers()
        }
      })
    },
    btnEditRow(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    handleEdit(row) {
      row.isEdit = false
    },
    async sureEdit(row) {
      if (!row.editRow.name && !row.editRow.description) {
        this.$message.error('内容必填')
        return
      }
      const res = await editRoles(row.editRow)
      console.log(res)
      this.$message.success(res.message)
      this.handleEdit(row)
      this.getUsers()
    },
    async delRow(id) {
      const res = await delRoles(id)
      console.log(res)
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        if (this.page > 1) {
          this.page--
        }
      }
      this.getUsers()
    },
    // 分配权限
    doAssign(id) {
      this.showAssignDialog = true
      this.currentId = id
      this.$nextTick(() => this.$refs.AssignPermission.getPermIds())
    }
  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
